package com.caac.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.caac.core.config.Config;
import com.caac.core.config.ViewClass;
import com.caac.core.dao.ViewPropDao;
import com.caac.core.dm.ViewProp;
import com.caac.core.service.ViewPropManager;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2004</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public class ViewPropManagerImpl extends BaseManagerImpl implements
        ViewPropManager {
    private ViewPropDao viewPropDao;

    public void setViewPropDao(ViewPropDao viewPropDao) {
        this.viewPropDao = viewPropDao;
    }

    public List getUnselectedViewPropsByGroupName(String groupName) {
        List all = getView(groupName).getChildren();
        List selected = getSelectedViewPropsByGroupName(groupName);
        List unselected = new ArrayList();
        for (int i = 0; i < all.size(); i++) {
            ViewProp vp = (ViewProp) all.get(i);
            if (!selected.contains(vp)) {
                unselected.add(vp);
            }
        }
        return unselected;
    }

    public List getSelectedViewPropsByGroupName(String groupName) {
        //List props = viewPropDao.getViewPropsByGroupName(groupName);
        //if (props == null || props.size() == 0) {
            return getView(groupName).getDefDmChildren();
        //}
        //return props;
    }

    public List getAllViewPropsByGroupName(String groupName) {
        return getView(groupName).getChildren();
    }

    public void saveViewProps(String groupName, String[] propNames) {
        viewPropDao.deleteViewPropsByGroupName(groupName);
        if (propNames != null) {
            for (int i = 0; i < propNames.length; i++) {
                String propName = propNames[i];
                if (propName != null) {
                    ViewProp p = new ViewProp();
                    p.setGroupName(groupName);
                    p.setPropName(propName);
                    p.setPriority(new Integer(i));
                    viewPropDao.saveViewProp(p);
                }
            }
        }
    }

    public List getUnselectedViewPropsByGroupName(String groupName,
                                                  String username) {
        List all = getView(groupName).getChildren();
        List selected = getSelectedViewPropsByGroupName(groupName, username);
        List unselected = new ArrayList();
        for (int i = 0; i < all.size(); i++) {
            ViewProp vp = (ViewProp) all.get(i);
            if (!selected.contains(vp)) {
                unselected.add(vp);
            }
        }
        return unselected;
    }

    public List getSelectedViewPropsByGroupName(String groupName,
                                                String username) {
        //List props = viewPropDao.getViewPropsByGroupName(groupName, username);
        //if (props == null || props.size() == 0) {
            return getView(groupName).getDefDmChildren();
        //}
        //return props;
    }

    public void saveViewProps(String groupName, String[] propNames,
                              String username) {
        viewPropDao.deleteViewPropsByGroupName(groupName, username);
        if (propNames != null) {
            for (int i = 0; i < propNames.length; i++) {
                String propName = propNames[i];
                if (propName != null) {
                    ViewProp p = new ViewProp();
                    p.setGroupName(groupName);
                    p.setPropName(propName);
                    p.setPriority(new Integer(i));
                    p.setUsername(username);
                    viewPropDao.saveViewProp(p);
                }
            }
        }
    }

    private static ViewClass getView(String groupName) {
        return Config.getInstance().getView(groupName);
    }
}
